package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"video-hub/models/mysql"

	"video-hub/videorpc/internal/svc"
	"video-hub/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserPerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserPerfectLogic {
	return &UserPerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 个人资料修改
func (l *UserPerfectLogic) UserPerfect(in *videorpc.UserPerfectRequest) (*videorpc.UserPerfectResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User
	err := findUser.FirstUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("该用户不存在")
	}

	user := &mysql.User{
		Model:     gorm.Model{ID: uint(in.UserId)},
		NickName:  in.NickName,
		HeadImage: in.HeadImage,
	}
	err = user.UpdateUser(mysql.DB)
	if err != nil {
		return nil, errors.New("信息修改失败")
	}
	return &videorpc.UserPerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
